home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Alles Voor Internet / Tout Pour Internet
/
alles voor internet.iso
/
MacInternet™
/
Telnet
/
NCSA
/
tn3270 2.4d7 source
/
tn3270
/
util.c
< prev
next >
Wrap
Text File
|
1992-04-18
|
13KB
|
582 lines
/*
* tn3270 for the Macintosh Source Code
* Brown University Computing and Information Services
* Version 2.4d7 April, 1992
* Copyright (c) 1988, 1989, 1990, 1991, 1992 by Brown University and by
* Peter John DiCamillo.
*
* Permission is granted to any individual or institution to use, copy,
* or redistribute the binary version of this software and its
* documentation provided this notice and the copyright notices are
* retained. Permission is granted to any individual or non-profit
* institution to use, copy, modify, or redistribute the source files
* of this software provided this notice and the copyright notices are
* retained. This software may not be distributed for profit, either
* in original form or in derivative works, nor can the source be
* distributed to other than an individual or a non-profit institution.
* Any individual or group interested in seeing and/or using these
* source files but who are prevented from doing so by the above
* constraints should contact Don Wolfe, Assistant Vice-President for
* Computer Systems at Brown University, (401) 863-7250, for possible
* software licensing of the source developed at Brown.
*
* Brown University and Peter John DiCamillo make no representations
* about the suitability of this software for any purpose.
*
* BROWN UNIVERSITY AND PETER JOHN DICAMILLO GIVE NO WARRANTY, EITHER
* EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
* INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#if !defined(USEDUMP)
#include "maclib.h"
#include "termdef.h"
#include "tn3270funcs.h"
#include "globals.h"
#else
#pragma load "tn3270DumpFile"
#endif
#pragma segment 3270seg2
extern char smallscreen; /* complete 24 x 80 window doesn't fit */
#define IAC 255 /* Telnet protocol interpret as command: */
#define DEBUGFILE "\pRam:tcpdebug"
#define DEBUGVOL "\pRam"
/* for debugging via serial port */
static IOParam pbo; /* serial output parms */
static unsigned char pbchr; /* output character */
static char debugopnflg = 0;
void tcpmemcpy(unsigned char *dest, unsigned char *src,
short *len, char tcpflg)
{
register short i, copylen;
if (tcpflg) {
if ((*len) == 0) return;
copylen = 0;
for (i=0; i < (*len); i++) {
if (src[i] == IAC) {
dest[copylen++] = IAC;
dest[copylen++] = IAC;
}
else {
dest[copylen++] = src[i];
}
}
(*len) = copylen;
}
else {
memcpy(dest, src, *len);
}
}
void GetGlobalRect(WindowPtr window, Rect *globalRect)
{
GrafPtr gp;
GetPort(&gp);
SetPort(window);
(*globalRect) = window->portRect;
LocalToGlobal((Point *)(&(globalRect->top)));
LocalToGlobal((Point *)(&(globalRect->bottom)));
SetPort(gp);
}
void defxtab(unsigned char **xtabinfo, unsigned char *xtab)
{
Size infosize;
register short i;
/* generate default table */
for (i=0; i < 256; i++) {
xtab[i] = i;
}
/* modify specified values */
infosize = GetHandleSize((Handle)xtabinfo);
/* ensure length is a multiple of two (as it should be) */
infosize >>= 1;
infosize <<= 1;
if (infosize < 2) return;
for (i=0; i < infosize; i += 2) {
xtab[(*xtabinfo)[i]] = (*xtabinfo)[i+1];
}
}
void getdeskrect(Rect *r)
{
static RgnHandle *GrayRgn = (RgnHandle *)0x9ee;
(*r) = (**GrayRgn)->rgnBBox;
}
unsigned char getmodel(short rows, short cols, cnr *cp)
{
/* note: for models 4 and 5, only 3278 is used, since
there are no real 3279-4 or -5 terminals */
if (colormac && (!cp->cs.nocolor)) {
if ((rows == 32) && (cols == 80)) return(MDL32793);
else if ((rows == 43) && (cols == 80)) return(MDL32784);
else if ((rows == 27) && (cols == 132)) return(MDL32785);
else return(MDL32792);
}
else {
if ((rows == 32) && (cols == 80)) return(MDL32783);
else if ((rows == 43) && (cols == 80)) return(MDL32784);
else if ((rows == 27) && (cols == 132)) return(MDL32785);
else return(MDL32782);
}
}
void adjmove(Rect *wr)
/* wr is the rectangle for a window in global coordinates */
/* the position of the rectangle is adjusted to fit on
on the screen as well as possible */
{
Rect r, devrect;
Point ctr;
GDHandle devgdev;
char found;
char devok;
short adj;
r = *wr; /* local copy of window rectange */
/* get center point and corresponding device rectange */
ctr.h = (r.left + r.right)/2;
ctr.v = (r.top + r.bottom)/2;
if (colormac) {
found = 0;
devgdev = GetDeviceList();
while (devgdev != 0) {
devrect = (*devgdev)->gdRect;
if (PtInRect(ctr, &devrect)) {
found = 1;
break;
}
else {
devgdev = GetNextDevice(devgdev);
}
}
if (!found) {
devgdev = GetMainDevice();
if (devgdev == 0) {
return; /* shouldn't happen */
}
else {
devrect = (*devgdev)->gdRect;
}
}
}
else {
devrect = qd.screenBits.bounds;
}
/* adjust for menu bar */
if ((devrect.top == 0) && (devrect.left == 0)) {
if (smallscreen) {
devrect.top = 4;
}
else {
devrect.top = 20;
}
}
/* return if window is entirely in device rectangle */
if ((devrect.top <= r.top) && (devrect.bottom >= r.bottom) &&
(devrect.left <= r.left) && (devrect.right >= r.right)) {
return;
}
if (colormac) {
/* check window will fit on device */
devok = ((devrect.bottom - devrect.top) >= (r.bottom - r.top)) &&
((devrect.right - devrect.left) >= (r.right - r.left));
/* if not, use entire desktop */
if (!devok) {
getdeskrect(&devrect);
if (devrect.top == 0) {
if (smallscreen) {
devrect.top = 4;
}
else {
devrect.top = 20;
}
}
if ((devrect.top <= r.top) && (devrect.bottom >= r.bottom) &&
(devrect.left <= r.left) && (devrect.right >= r.right)) {
return;
}
}
}
/* following order ensures that top left corner is in window */
/* make right edge fit */
adj = r.right - devrect.right;
if (adj > 0) {
r.right -= adj;
r.left -= adj;
}
/* make bottom edge fit */
adj = r.bottom - devrect.bottom;
if (adj > 0) {
r.bottom -= adj;
r.top -= adj;
}
/* make left edge fit */
adj = devrect.left - r.left;
if (adj > 0) {
r.left += adj;
r.right += adj;
}
/* make top edge fit */
adj = devrect.top - r.top;
if (adj > 0) {
r.top += adj;
r.bottom += adj;
}
(*wr) = r;
}
void def_sessname(cnr *cp)
{
unsigned char newname[256];
short i, myseq;
cnr *listcp;
if (strlen(cp->cswtitle) > 0) {
strcpy(newname, cp->cswtitle);
}
else if (strlen(cp->s_fName) > 0) {
strcpy(newname, cp->s_fName);
}
else {
strcpy(newname, cp->hostonly);
}
newname[249] = 0; /* truncate if necessary */
strcpy(cp->sessname, newname);
myseq = 0;
for (i=0; i < MAXSESSIONS; i++) {
listcp = cplist[i];
if ((listcp != 0) && (listcp != cp)) {
if (strcmp(listcp->sessname, cp->sessname) == 0) {
if (listcp->sessnameseq == 0) {
listcp->sessnameseq = 1;
get_sessname(newname, listcp);
c2pstr(newname);
if (listcp->myWindow != 0) {
SetWTitle(listcp->myWindow, newname);
}
myseq = 2;
break;
}
else {
if (myseq < (listcp->sessnameseq+1)) {
myseq = listcp->sessnameseq+1;
}
}
}
}
}
cp->sessnameseq = myseq;
}
void get_sessname(unsigned char *sname, cnr *cp)
{
if (cp->sessnameseq == 0) {
strcpy(sname, cp->sessname);
}
else {
sprintf(sname, "%s %d", cp->sessname, cp->sessnameseq);
}
}
GDHandle myGetMaxDevice(Rect *globalRect)
{
GDHandle GDTemp;
GDTemp = GetMaxDevice(globalRect);
if (GDTemp != 0) return(GDTemp);
return(GetMainDevice());
}
short cfmapsize(char which, char color)
{
short dflthsize, dfltvsize;
short althsize, altvsize;
short hpixsize, vpixsize;
unsigned short rowBytes, bwsize, colorsize;
long mapsize;
Rect deskrect;
GDHandle currGD;
PixMapHandle currPM;
/* calculate maximum hpixsize and vpixsize */
if (cf_dfltptsize == 9) {
dflthsize = 80 * 6 + 10;
dfltvsize = 24 * 12 + 26;
}
else {
dflthsize = 80 * 7 + 10;
dfltvsize = 24 * 16 + 26;
}
if (cf_altptsize == 9) {
althsize = cf_altcols * 6 + 10;
altvsize = cf_altrows * 12 + 26;
}
else {
althsize = cf_altcols * 7 + 10;
altvsize = cf_altrows * 16 + 26;
}
if (dflthsize > althsize) {
hpixsize = dflthsize;
}
else {
hpixsize = althsize;
}
if (dfltvsize > altvsize) {
vpixsize = dfltvsize;
}
else {
vpixsize = altvsize;
}
if (which == 0) { /* text bitmap for color */
if (!colormac) return(0);
rowBytes = (((hpixsize + 31) >> 5) + 1) << 2;
mapsize = vpixsize;
mapsize = mapsize * rowBytes + 4;
bwsize = (mapsize + 1023) / 1024;
getdeskrect(&deskrect);
currGD = myGetMaxDevice(&deskrect);
currPM = (*currGD)->gdPMap;
rowBytes = ((((hpixsize * (*currPM)->pixelSize) + 31) >> 5) + 1) << 2;
mapsize = vpixsize;
mapsize = mapsize * rowBytes + 4;
colorsize = (mapsize + 1023) / 1024;
return(colorsize - bwsize);
}
else { /* graphics bitmap */
if (color && colormac) {
getdeskrect(&deskrect);
currGD = myGetMaxDevice(&deskrect);
currPM = (*currGD)->gdPMap;
rowBytes = ((((hpixsize * (*currPM)->pixelSize) + 31) >> 5) + 1) << 2;
mapsize = vpixsize - 19;
mapsize = mapsize * rowBytes + 4;
colorsize = (mapsize + 1023) / 1024;
return(colorsize);
}
else {
rowBytes = (((hpixsize + 31) >> 5) + 1) << 2;
mapsize = vpixsize - 19;
mapsize = mapsize * rowBytes + 4;
bwsize = (mapsize + 1023) / 1024;
return(bwsize);
}
}
}
writeinfo *new_writeinfo(cnr *cp)
{
short i;
writeinfo *w;
for (i=0; i < WRITEINFOCOUNT-1; i++) {
cp->writesave[i] = cp->writesave[i+1];
}
w = &(cp->writesave[WRITEINFOCOUNT-1]);
memset(w, 0, sizeof(writeinfo));
cp->writesave[WRITEINFOCOUNT-1].seqnum =
cp->writesave[WRITEINFOCOUNT-2].seqnum + 1;
return(w);
}
void show_writeinfo(cnr *cp)
{
static unsigned long * Ticks = (unsigned long *)0x16a;
char dbsave;
writeinfo *w;
unsigned long currticks, elapsed, duration;
short i, j, datalength;
unsigned char hexstr[8];
if (cp == 0) return;
dbsave = cf_dblevel;
cf_dblevel = 1;
if (writedebug == 0) {
writedebug = 1;
dbdlg("Write tracing now enabled.");
cf_dblevel = dbsave;
return;
}
currticks = *Ticks;
for (i=0; i < WRITEINFOCOUNT; i++) {
w = &(cp->writesave[i]);
if (w->seqnum != 0) {
elapsed = (currticks - w->start_ticks + 30)/60;
duration = (w->end_ticks - w->start_ticks);
sprintf(dbgmsg, "#%d: %ld sec. ago for %ld ticks, write calls = %d, length = %d, start = \"",
w->seqnum, elapsed, duration, w->calls, w->length);
datalength = w->length;
if (datalength > INFODATASIZE) datalength = INFODATASIZE;
for (j=0; j < datalength; j++) {
if (strlen(dbgmsg) > 250) break;
if (j == 0) {
sprintf(hexstr, "%x", w->data[j]);
}
else {
sprintf(hexstr, " %x", w->data[j]);
}
strcat(dbgmsg, hexstr);
}
strcat(dbgmsg, "\"");
dbdlg(dbgmsg);
}
}
cf_dblevel = dbsave;
}
void putln (unsigned char *s) /* put message in error log */
{
static FILE *fp = 0;
short fref, i;
long count;
static int rc;
if (!cf_dblevel) return;
if (cf_dblevel == 99) {
if (!debugopnflg) {
rc = seropen();
debugopnflg = 1;
}
if (rc != 0) return;
count = strlen(s);
if (count > 0)
for (i = 0; i < count; i++) {
rc = serchr(s[i]);
if (havetcp) myStask();
if (rc != 0) break;
}
if (rc == 0) rc = serchr('\015');
if (rc == 0) rc = serchr('\012');
dbgwait();
}
else {
if (!debugopnflg) {
FSDelete(DEBUGFILE, 0);
Create(DEBUGFILE, 0, 'MPS ', 'TEXT');
debugopnflg = 1;
}
if (FSOpen(DEBUGFILE, 0, &fref) != 0) return;
SetFPos(fref, fsFromLEOF, 0L);
count = strlen(s);
if (count > 0) FSWrite(fref, &count, s);
count = 1;
FSWrite(fref, &count, "\015");
FSClose(fref);
/* FlushVol(DEBUGVOL, 0); */
}
}
OSErr seropen(void)
{
static unsigned char outname[8];
unsigned char inname[8];
short refnum;
char port = 0; /* 0 = modem port, 1 = printer port */
OSErr rc;
memset(&pbo, 0, sizeof(IOParam));
if (port == 0) {
strcpy(inname,".AIn");
strcpy(outname,".AOut");
pbo.ioRefNum = aoutRefNum;
}
else {
strcpy(inname,".BIn");
strcpy(outname,".BOut");
pbo.ioRefNum = boutRefNum;
}
c2pstr(inname);
c2pstr(outname);
rc = OpenDriver(outname, &refnum);
if (rc == noErr) rc = OpenDriver(inname, &refnum);
pbo.ioNamePtr = (StringPtr)outname;
pbo.ioVersNum = 0;
pbo.ioPermssn = fsWrPerm;
pbo.ioMisc = 0;
pbo.ioBuffer = (Ptr)&pbchr;
pbo.ioReqCount = 1;
pbo.ioPosMode = 0;
pbo.ioPosOffset = 0;
return(rc);
}
OSErr serchr(unsigned char c)
{
pbchr = c;
return(PBWrite((ParmBlkPtr)&pbo, 0));
}
void dbgdump(unsigned char *msg, unsigned char *data, short len)
{
short count, i, j;
unsigned char dbgchar[66];
sprintf(dbgmsg, "%s: %d bytes:", msg, len);
putln(dbgmsg);
count = 0;
dbgmsg[0] = 0;
dbgchar[0] = 0;
for (i=0; i < len; i++) {
j = strlen(dbgmsg);
sprintf(dbgmsg+j, "%02x", data[i]);
if (xtabh != 0) {
j = strlen(dbgchar);
sprintf(dbgchar+j, "%c ", (*xtabh)[data[i]]);
}
count++;
if (count == 32) {
putln(dbgmsg);
if (xtabh != 0) putln(dbgchar);
dbgmsg[0] = 0;
dbgchar[0] = 0;
count = 0;
}
}
if (strlen(dbgmsg) > 0) putln(dbgmsg);
if ((strlen(dbgchar) > 0) && (xtabh != 0)) putln(dbgchar);
putln("");
}
void dbgwait(void)
{
static unsigned long *Ticks = (unsigned long *)0x16a;
unsigned long limit;
limit = (*Ticks) + 15;
while (limit > (*Ticks)) if (havetcp) myStask();
}